package com.kzj.laimeimei.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.GBaseActivity;
import com.kzj.laimeimei.activity.MainActivity;
import com.kzj.laimeimei.beans.UserBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.CardItemView;
import com.kzj.laimeimei.recyclerCard.card.other.LoginCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;

/**
 * Created by xyg on 2015/10/26.
 */
public class LoginCardView extends CardItemView<LoginCard> {
    private EditText edit_phone;
    private TextView textView_send_code;
    private EditText edit_code;
    private TextView textView_login;
    private Context context;
    private int sendTime = 180000;

    public LoginCardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LoginCardView(Context context) {
        super(context);
        this.context = context;
    }

    public LoginCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public void build(final LoginCard card) {
        super.build(card);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
        textView_send_code = (TextView) findViewById(R.id.textView_send_code);
        edit_code = (EditText) findViewById(R.id.edit_code);
        textView_login = (TextView) findViewById(R.id.textView_login);
        textView_send_code.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit_phone.getText() != null && !edit_phone.getText().toString().equals("")) {
                    TaskSendCode(edit_phone.getText().toString());
                    textView_send_code.setClickable(false);
                } else {
                    App.showToast("手机号码不能为空");
                }
            }
        });
        textView_login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit_phone.getText() != null && !edit_phone.getText().toString().equals("") &&
                        edit_code.getText() != null && !edit_code.getText().toString().equals("")) {
                    TaskLogin(edit_phone.getText().toString(), edit_code.getText().toString());
                    textView_login.setClickable(false);
                } else {
                    App.showToast("手机号码/验证码不能为空");
                }
            }
        });
    }

    private void TaskSendCode(String phone) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) context, null, null, false, Constant.getRootUrl() + Constant.Action.Action_Send_Code,
                RequestServerManager.getInstance().getRequestParams(Constant.Action.Action_Send_Code, phone), BaseResponse.class,
                new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        textView_send_code.setClickable(false);
                        new CountDownTimer(sendTime, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                textView_send_code.setText(millisUntilFinished / 1000 + "秒后重发");
                            }

                            @Override
                            public void onFinish() {
                                textView_send_code.setClickable(true);
                                textView_send_code.setText("发送验证码");
                            }
                        }.start();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.showToast(errorMsg);
                        textView_send_code.setClickable(true);
                    }
                });
    }


    private void TaskLogin(String user_name, String verify_code) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) context, null, null, false, Constant.getRootUrl() + Constant.Action.Action_Login,
                RequestServerManager.getInstance().getRequestParams(Constant.Action.Action_Login, user_name, verify_code), BaseResponse.class,
                new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        textView_login.setClickable(true);
                        JSONObject jsonObject = (JSONObject) result.getRetval();
                        UserBean userBean = JSON.parseObject(jsonObject.toString(), UserBean.class);
                        App.getInstance().setUserBean(userBean);
                        if (getCard().getFlag() != null && getCard().getFlag().equals("service")) {
                            ((GBaseActivity) context).finish();
                        } else {
                            Intent intent = new Intent(context, MainActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            context.startActivity(intent);
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.showToast(errorMsg);
                        textView_login.setClickable(true);
                    }
                });
    }
}
